var app = getApp()

Page({
  data: {
    server: app.api.server,
    preface_url: app.api.server + '/images/paper_preface.png',
    currentpage: 0,
    progresspercent: 0,
    survey: null,
    firstques: 0,
    doingques:false,
    quessize: 0,
    pagesize: 2,
    answerrecordid: 0,
    shownextpage: false,
    answers: [],
    columns: [],
  },
  checknext: function() {
    this.setData({
      shownextpage: this.data.firstques - 1 + this.data.pagesize < this.data.quessize
    })
  },
  nextpage: function() {
    var that = this

    if (app.globalData.userInfo == null) {
      app.getUserInfo()
      that.setData({
        currentpage: that.data.currentpage + 1
      })

    } else {
      that.setData({
        currentpage: that.data.currentpage + 1
      })
    }
    //判断最近1个月 是否有未完成记录，显示跳转到最新题目的按钮
    if(that.data.currentpage == 1){
      app.api.api_paper_getdoingsurvey({
        surveyid: this.data.survey.surveyid
      }).then(res => {
        this.setData({
          firstques: res.firstques,
          doingques: res.doingques,
          answerrecordid: res.answerrecordid,
        })
        if(res.doingques){
          that.showsurvay({ id: this.data.survey.surveyid });
        }
      })
    }

    // wx.getStorage({
    //   key: 'notfirstlogin',
    //   success: function (res) {
    //     // success
    //     if (app.globalData.userInfo == null) {
    //       app.getUserInfo()
    //       that.setData({
    //         currentpage: that.data.currentpage + 1
    //       })

    //     }
    //     else {
    //       that.setData({
    //         currentpage: that.data.currentpage + 1
    //       })
    //     }
    //   },
    //   fail: function () {
    //     // fail
    //     wx.navigateTo({
    //       url: '../login/login',
    //       success: function (res) {
    //         // success
    //       },
    //       fail: function () {
    //         // fail
    //       },
    //       complete: function () {
    //         // complete
    //       }
    //     })
    //   },
    //   complete: function () {
    //     // complete
    //   }
    // })



  },
  prepage: function() {
    this.setData({
      currentpage: this.data.currentpage - 1
    })
  },
  singleChoice: function() {
    this.setData({
      progresspercent: this.data.currentpage / this.data.survey.queses.length,
      currentpage: this.data.currentpage + 1,
    })
  },
  complete: function(e) {
    console.log(e)
    var createrecord = false;
    var finishrecord = false;
    var myanswers = []
    for (var i = 0; i < this.data.survey.queses.length; i++) {
      var key = "ques" + this.data.survey.queses[i].quesorder;
      if (this.data.survey.queses[i].quesorder === 1){
        createrecord = true;
      }
      if (this.data.survey.queses[i].quesorder === this.data.quessize){
        finishrecord = true;
      }
      if (this.data.survey.queses[i].questype.typeid == 3) {
        var answerkey = "answer" + this.data.survey.queses[i].quesorder;
        myanswers = myanswers.concat({
          optionid: parseInt(e.detail.value[key]),
          answercontent: e.detail.value[answerkey]
        })
      } else {
        if (parseInt(e.detail.value[key])){
          myanswers = myanswers.concat({
            optionid: parseInt(e.detail.value[key]),
            answercontent: ""
          })
        }
      }

    }
    this.setData({
      answers: myanswers
    })

    if (this.data.survey.queses.length > this.data.answers.length){
      wx.showToast({
        title: '亲，请完成所有题目哦',
        icon: 'none',
        duration: 2000
      })
      return;
    }

    //第一题时，创建答题记录
    if (createrecord){
      app.api.api_paper_saveanswerrecore({
        surveyid: this.data.survey.surveyid
      }).then(res => {
        this.setData({
          answerrecordid: res.answerrecordid
        })
        this.realcomplate()
      })
    }else{
      if (finishrecord){
        app.api.api_paper_saveanswerrecore({
          answerrecordid: this.data.answerrecordid,
          surveyid: this.data.survey.surveyid
        }).then(res => {

        });
      }
      this.realcomplate()
    }
   
  },
  realcomplate:function(){
    var that = this
    wx.showToast({
      title: '提交中',
      icon: 'loading',
      duration: 10000
    })

    wx.request({
      url: this.data.server + '/completeSurvey.action',
      data: {
        userid: app.globalData.userInfo.userid,
        answerrecordid: this.data.answerrecordid,
        optionList: this.data.answers,
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        // success
        //还有其他题目
        wx.hideToast()
        if (that.data.shownextpage) {
          //设置刷新参数，去除提示
          that.setData({ firstques: that.data.firstques + that.data.pagesize, doingques: false });
          that.showsurvay({ id: that.data.survey.surveyid });
        } else {
          that.setData({
            currentpage: parseInt(that.data.currentpage + 1),
          })
          wx.setNavigationBarTitle({
            title: '完成',
            success: function (res) {
              // success
            }
          })
        }

      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  navback: function() {
    wx.navigateBack({
      delta: 1, // 回退前 delta(默认为1) 页面
    })
  },
  showsurvay: function (param) {
    var that = this;
    wx.request({
      url: this.data.server + '/showSurvey.action',
      data: {
        firstques: this.data.firstques,
        surveyid: param.id,
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        // success
        that.data.survey = res.data.surveyresponse
        that.setData({
          survey: res.data.surveyresponse
        })
        that.checknext()
        wx.hideNavigationBarLoading()

      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },

  onLoad: function(param) {
    wx.showNavigationBarLoading()
    var that = this;

    app.api.api_meeting_enum({
      key: 'quescolumns',
      type: 'array'
    }).then(res => {
      this.setData({
        columns: res
      })
    })

    app.api.api_paper_quessize({
      surveyid: param.id
    }).then(res => {
      this.setData({
        quessize: res
      })
    })

    that.showsurvay(param)
  }
})